/**
 * Created by Administrator on 2017/3/7 0007.
 */
var Page2= {
    create: function () {
        var page = {};

        var tw_bga, tw_bgb, tw_whole;
        var arr_twEnd0=[];
        var arr_twEnd1=[];
        var bl_end=false;//防止最后瞎点重复循环

        page.ui = new PIXI.DisplayObjectContainer();
        var bg = new PIXI.Sprite(resource['assets/images/pageInfo_bg.png'].texture);
        bg.alpha = 1;
        page.ui.addChild(bg);
        bg.x=-1136*0.3;
        bg.y=-640*0.15;
        bg.scale.x=2.3;
        bg.scale.y=2.3;
        page.ui.scale.x=0.7;
        page.ui.scale.y=0.7;
        page.ui.x=1136*0.15;
        page.ui.y=640*0.15;

        var arr_img=[]
        var arr_imgP0=[
            {x:382-70,y:33-70},
            {x:290-70,y:221},
            {x:397,y:513+75},
            {x:556+70,y:394+70},
            {x:767+70,y:178-70}
        ];
        var arr_imgP1=[
            {x:382,y:33},
            {x:290,y:221},
            {x:397,y:513},
            {x:556,y:394},
            {x:767,y:178}
        ]

        // var img0=new PIXI.Sprite(resource['assets/images/page2_i0.png'].texture);
        // img0.anchor.set(0.5);
        // img0.x=arr_imgP0[i].x;
        // img0.y=arr_imgP0[i].y;
        // img0.alpha = 1;


        for(var i=0;i<5;i++) {
            var img = new PIXI.Sprite(resource['assets/images/page2_i' + i.toString() + '.png'].texture);
            img.x = arr_imgP0[i].x;
            img.y = arr_imgP0[i].y;
            img.alpha = 0;
            img.scale.x=1;
            img.scale.y=1;
            page.ui.addChild(img);
            arr_img.push(img);

            tweenIn(i);
        }
        function tweenIn(idx) {
            new TWEEN.Tween({x:arr_imgP0[idx].x,y:arr_imgP0[idx].y,alpha:0})
                    .to({x:arr_imgP1[idx].x,y:arr_imgP1[idx].y,alpha:1},1500)
                    .onUpdate(function () {
                        arr_img[idx].x=this.x;
                        arr_img[idx].y=this.y;
                        arr_img[idx].alpha=this.alpha;
                    })
                    .delay(150*idx)
                    .easing(TWEEN.Easing.Back.Out)
                    .start();
            var tw= new TWEEN.Tween({x:arr_img[idx].x,y:arr_img[idx].y,alpha:1})
                .to({x:1136/2,y:740/2,alpha:0},1000)
                .onUpdate(function () {
                    arr_img[idx].alpha=this.alpha;
                    // arr_img[idx].x=this.x;
                    // arr_img[idx].y=this.y;
                })
                .delay(100*i)
                .easing(TWEEN.Easing.Cubic.Out)
            arr_twEnd0.push(tw);
        }

        var arr_imgT=[];
        var arr_imgTP0=[
            {x:812,y:140},
            {x:329,y:184},
            {x:231,y:492},
            {x:644,y:588},
            {x:764,y:341}
        ];
        for(var i=0;i<5;i++)
        {
            var img=new PIXI.Sprite(resource['assets/images/page2_t'+i.toString()+'.png'].texture);
            img.anchor.set(0.5);
            img.x=arr_imgTP0[i].x;
            img.y=arr_imgTP0[i].y;
            img.scale.x=img.scale.y=0;
            img.alpha = 0;
            page.ui.addChild(img);
            arr_imgT.push(img);

            tweenInT(i);
            // new TWEEN.Tween({scaleX:0,scaleY:0,alpha:0})
            //     .to({scaleX:1,scaleY:1,alpha:1},700)
            //     .onUpdate(function () {
            //         img.scale.x=this.scaleX;
            //         img.scale.y=this.scaleY;
            //         img.alpha=this.alpha;
            //     })
            //     .delay(1000+100*i)
            //     .easing(TWEEN.Easing.Back.Out)
            //     .start();
        }
        function tweenInT(idx) {
            new TWEEN.Tween({scaleX:0,scaleY:0,alpha:0})
                .to({scaleX:1,scaleY:1,alpha:1},1200)
                .onUpdate(function () {
                    arr_imgT[idx].scale.x=this.scaleX;
                    arr_imgT[idx].scale.y=this.scaleY;
                    arr_imgT[idx].alpha=this.alpha;
                })
                .delay(100*idx)
                .easing(TWEEN.Easing.Back.Out)
                .start();

            var tw= new TWEEN.Tween({scaleX:1,scaleY:1,x:arr_imgT[idx].x,y:arr_imgT[idx].y,alpha:1})
                .to({x:1136/2,y:740/2,scaleX:0,scaleY:0,alpha:0},800)
                .onUpdate(function () {
                    // arr_imgT[idx].x=this.x;
                    // arr_imgT[idx].y=this.x;
                    arr_imgT[idx].scale.x=this.scaleX;
                    arr_imgT[idx].scale.y=this.scaleY;
                    arr_imgT[idx].alpha=this.alpha;
                })
                .delay(idx*100)
                .easing(TWEEN.Easing.Back.In)
            arr_twEnd1.push(tw);
        }

        var imgFrame=new PIXI.Sprite(resource['assets/images/page2_frame.png'].texture);
        imgFrame.anchor.set(0.5);
        imgFrame.x=568;
        imgFrame.y=391;
        imgFrame.alpha = 0;
        imgFrame.scale.x=1;
        imgFrame.scale.y=1;
        page.ui.addChild(imgFrame);

        var t5=new PIXI.Sprite(resource['assets/images/page2_t5.png'].texture);
        t5.anchor.set(0.5);
        t5.x=529;
        t5.y=577;
        t5.alpha = 0;
        page.ui.addChild(t5);

        var click=new PIXI.Sprite(resource['assets/images/page2_click.png'].texture);
        click.anchor.set(0.5);
        click.x=532;
        click.y=320;
        click.alpha = 0;
        click.on('pointerdown',function (e) {
            if(!bl_end) {
                click.interactive = false;
                var event = new CustomEvent("Main_Page3In");
                document.dispatchEvent(event);
                bl_end=true;
            }
        })
        page.ui.addChild(click);


        setTimeout(function () {
            for(var i=0;i<arr_twEnd0.length;i++)
            {
                arr_twEnd0[i].start();
            }
            for(var i=0;i<arr_twEnd1.length;i++)
            {
                arr_twEnd1[i].start();

            }
            new TWEEN.Tween({alpha:0})
                .to({alpha:1},1000)
                .onUpdate(function () {
                    imgFrame.alpha=this.alpha;
                })
                .easing(TWEEN.Easing.Cubic.Out)
                .start();

            new TWEEN.Tween({alpha:0,scaleX:0,scaleY:0})
                .to({alpha:1,scaleX:1,scaleY:1},1000)
                .onUpdate(function () {
                    t5.alpha=this.alpha;
                    t5.scale.x=this.scaleX;
                    t5.scale.y=this.scaleY;
                })
                .delay(500)
                .easing(TWEEN.Easing.Cubic.Out)
                .start();

            new TWEEN.Tween({alpha:0,scaleX:0,scaleY:0})
                .to({alpha:1,scaleX:1,scaleY:1},1000)
                .onUpdate(function () {
                    click.alpha=this.alpha;
                    click.scale.x=this.scaleX;
                    click.scale.y=this.scaleY;
                })
                .onComplete(function () {
                    click.interactive=true;
                    new TWEEN.Tween({alpha:1,scaleX:1,scaleY:1})
                        .to({alpha:0.9,scaleX:0.9,scaleY:0.9},1000)
                        .onUpdate(function () {
                            click.alpha=this.alpha;
                            click.scale.x=this.scaleX;
                            click.scale.y=this.scaleY;
                        })
                        .onComplete(function () {

                            
                        })
                        .yoyo(true)
                        .repeat(Infinity)
                        .start();
                })
                .delay(500)
                .easing(TWEEN.Easing.Cubic.Out)
                .start();
        },3000);

        return page;
    }
}
